//  Created by WHC on 18/4/10.
//  Copyright © 2017年 WHC. All rights reserved.
//
//  Github <https://github.com/netyouli/react-native-whc-calendar>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import React, {Component} from 'react';
import {
    View,
    StyleSheet,
    TouchableHighlight,
    Text,
    Dimensions,
} from 'react-native';

const screenWidth = Dimensions.get('window').width;

export default class CalendarGridItem extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        const {
            item = null,
            onPress = null,
            startSelectedHint = null,
            endSelectedHint = null,
            selectedColor = '#1895D4',
            selectedMidColor = 'rgba(24,149,212,0.2)',
            selectedTextColor = '#fff',
            highlightColor = 'red',
            bottomHintForDate = null,
          index=0
        } = this.props;

        if (!item) {
            return <View style={styles.button}/>
        }

        const {
            year,
            month,
            week,
            weekText,
            holiday,
            isToday,
            isOver,
            isLessToday,
            startSelected,
            endSelected,
            isSelected,
            day,
        } = item;

        let holidayValue = holiday;
        if (isToday) {
            holidayValue = '今天';
        }
        const didSelected = startSelected || endSelected;
        let dayTextColor = didSelected ? selectedTextColor : (isLessToday || isOver ? '#aaa' : '#333');
        let holidayTextColor = didSelected ? selectedTextColor : (isLessToday || isOver ? '#aaa' : highlightColor);
        let backColor = '#fff';
        if (!isLessToday && !isOver && (week === 0 || week === 6)) {
            dayTextColor = didSelected ? selectedTextColor : highlightColor;
        }
        if (startSelected || endSelected) {
            backColor = selectedColor;
            if (startSelectedHint && startSelectedHint !== '' && startSelected) {
                holidayValue = startSelectedHint;
            }else if (endSelectedHint && endSelectedHint !== '' && endSelected) {
                holidayValue = endSelectedHint;
            }
        }else if (isSelected) {
            backColor = selectedMidColor;
        }
        const hint = bottomHintForDate ? bottomHintForDate(item, index) : null;
        const contents = (
            <View style={styles.buttonView}>
                <Text style={[styles.buttonHolidayText, {color: holidayTextColor}]}>{holidayValue}</Text>
                <Text style={[styles.buttonText, {color: dayTextColor}]}>{day}</Text>
                <Text style={[styles.buttonBottomText, {color: dayTextColor}]}>{hint || ''}</Text>
            </View>
        );
        if (isLessToday || isOver) {
            backColor = 'rgb(252,252,252)';
            return (
                <View style={[styles.button, {backgroundColor: backColor}]}>
                    {contents}
                </View>
            );
        }
        return (
            <TouchableHighlight
                style={[styles.button, {backgroundColor: backColor}]}
                enabled={isLessToday}
                underlayColor={'#ccc'}
                onPress={() => {
                    onPress && onPress(item);
                }}>
                {contents}
            </TouchableHighlight>
        );
    }
}

const styles = StyleSheet.create({
    button: {
        alignItems: 'center',
        backgroundColor: '#fff',
        width: screenWidth / 7.0 - 0.1,
        height: 60,
        marginBottom: 0.5,
    },
    buttonView: {
        width: screenWidth / 7.0 - 0.1,
        height: 60,
    },
    buttonHolidayText: {
        position: 'absolute',
        color: 'red',
        textAlign: 'center',
        fontSize: 10,
        top: 10,
        left: 0,
        right: 0,
    },
    buttonText: {
        position: 'absolute',
        color: '#333',
        textAlign: 'center',
        left: 0,
        right: 0,
        bottom: 20,
        fontWeight: 'bold',
    },
    buttonBottomText: {
        position: 'absolute',
        color: '#888',
        textAlign: 'center',
        left: 0,
        right: 0,
        bottom: 0,
        fontWeight: '500',
    },
});
